/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import ru.rantipin.media.MediaHelper;
import ru.rantipin.media.MediaProcessor;
import ru.rantipin.media.MediaProcessorException;

/**
 *
 * @author rantipin
 */
public class MediaPanel extends JPanel{

    public MediaPanel() {
        
    }
    
    private void initComponentsLayout() {
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);
        
        gridbag.setConstraints(getUseBWShort(), 
                new GridBagConstraints(
                    0, 0,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 5, 0, 5),
                    0, 0
                ));
        add(getUseBWShort());
        
        gridbag.setConstraints(getGroupPanel(), 
                new GridBagConstraints(
                    0, 1,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 5, 0, 5),
                    0, 0
                ));
        add(getGroupPanel());
        
        if (visualComponent != null) {
            gridbag.setConstraints(visualComponent, 
                new GridBagConstraints(
                    0, 2,
                    1, 1,
                    1.0, 1.0,
                    GridBagConstraints.CENTER, GridBagConstraints.NONE,
                    new Insets(5, 5, 0, 5),
                    0, 0
                ));
            add(visualComponent);
        }
        
        if (controlComponent != null) {
            gridbag.setConstraints(controlComponent, 
                new GridBagConstraints(
                    0, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 5, 0, 5),
                    0, 0
                ));
            add(controlComponent);
        }
    }
    
    public void showPanel() {
        try {
            getUseBWShort().setSelected(false);
            getLeftRadioButton().setSelected(true);
            visualComponent = MediaHelper.getInstance().getVisualComponent();
            controlComponent = MediaHelper.getInstance().getControlComponent();
            initComponentsLayout();
            
            int height = 0;
            Component[] comp = getComponents();
            for(int i = 0; i < comp.length; i++) {
                height += comp[i].getPreferredSize().height;
            }
            
            setMinimumSize(new Dimension(0, height + 20));
        } catch (MediaProcessorException ex) {
            String message = "Can't show panel with media data";
            Logger.getLogger(MediaPanel.class.getName()).log(Level.SEVERE, message, ex);
            JOptionPane.showMessageDialog(MediaPanel.this, ex.getMessage(), 
                            "Initialize media frame error", JOptionPane.ERROR_MESSAGE);
        }
    }
    public void clearPanel() {
        remove(getGroupPanel());
        remove(getUseBWShort());
        if (visualComponent != null) {
            remove(visualComponent);
            visualComponent = null;
        }
        if (controlComponent != null) {
            remove(controlComponent);
            controlComponent = null;
        }
    }
    
    private JRadioButton getLeftRadioButton() {
        if (left == null) {
            left = new JRadioButton("Left", true);
            left.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MediaHelper.getInstance().setMediabounds(MediaProcessor.MediaBounds.LEFT);
                }
            });
            group.add(left);
        }
        
        return left;
    }
    
    private JRadioButton getRightRadioButton() {
        if (right == null) {
            right = new JRadioButton("Right", false);
            right.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MediaHelper.getInstance().setMediabounds(MediaProcessor.MediaBounds.RIGHT);
                }
            });
            group.add(right);
        }
        
        return right;
    }
    
    private JRadioButton getTopRadioButton() {
        if (top == null) {
            top = new JRadioButton("Top", false);
            top.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MediaHelper.getInstance().setMediabounds(MediaProcessor.MediaBounds.TOP);
                }
            });
            group.add(top);
        }
        
        return top;
    }
    
    private JRadioButton getBottomRadioButton() {
        if (bottom == null) {
            bottom = new JRadioButton("Bottom", false);
            bottom.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    MediaHelper.getInstance().setMediabounds(MediaProcessor.MediaBounds.BOTTOM);
                }
            });
            group.add(bottom);
        }
        
        return bottom;
    }
    
    private JPanel getGroupPanel() {
        if (radioGroup == null) {
            radioGroup = new JPanel();
            radioGroup.setBorder(BorderFactory.createTitledBorder("Scanning borders"));
            radioGroup.add(getLeftRadioButton());
            radioGroup.add(getRightRadioButton());
            radioGroup.add(getTopRadioButton());
            radioGroup.add(getBottomRadioButton());
        }
        
        return radioGroup;
    }
    
    private JCheckBox getUseBWShort() {
        if (useBWShort == null) {
            useBWShort = new JCheckBox("Use forced black and white", false);
            useBWShort.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    MediaHelper.getInstance().setUseForceBW(useBWShort.isSelected());
                }
            });
        }
        
        return useBWShort;
    }
    
    private JCheckBox useBWShort = null;
    private Component visualComponent = null;
    private Component controlComponent = null;
    private JRadioButton left = null;
    private JRadioButton right = null;
    private JRadioButton top = null;
    private JRadioButton bottom = null;
    private JPanel radioGroup = null;
    private ButtonGroup group = new ButtonGroup();

}
